package cn.wolfcode.web.controller;

import cn.wolfcode.domain.Appointment;
import cn.wolfcode.domain.Business;
import cn.wolfcode.domain.SystemDictionary;
import cn.wolfcode.enums.AppointmentStatusEnum;
import cn.wolfcode.qo.AppointmentQueryObject;
import cn.wolfcode.service.IAppointmentService;
import cn.wolfcode.service.IBusinessService;
import cn.wolfcode.service.ISystemDictionaryService;
import cn.wolfcode.util.RequiredPermission;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
@RequestMapping("/appointment")
public class AppointmentController {
    @Autowired
    private IAppointmentService  appointmentService;
    @Autowired
    private ISystemDictionaryService systemDictionaryService;
    @Autowired
    private IBusinessService businessService;
    @RequestMapping("/list")
    @RequiredPermission(name="预约查询",expression = "appointment:list")
    public String list(Model model,@ModelAttribute("qo") AppointmentQueryObject qo){

        List<SystemDictionary> systemDictionaries = systemDictionaryService.queryBySn("business");
        model.addAttribute("systemDictionaries",systemDictionaries);
        List<Business> businesses = businessService.listAll();
        model.addAttribute("businesses",businesses);
        model.addAttribute("appointStatusEnums", AppointmentStatusEnum.values());

        PageInfo<Appointment> pageInfo = appointmentService.query(qo);
        model.addAttribute("pageInfo",pageInfo);
        return "appointment/list";

    }
    @RequestMapping("/delete")
    @RequiredPermission(name="预约删除",expression = "appointment:delete")
    public String delete(Long id){
        if (id!=null) {
            appointmentService.delete(id);
        }
        return "redirect:/appointment/list";  //让浏览器重新发一次请求
    }
    @RequestMapping("/input")
    @RequiredPermission(name="预约新增或修改",expression = "appointment:input")
    public String input(Long id,Model model){
        if (id!=null) {
            Appointment appointment = appointmentService.get(id);
            model.addAttribute("appointment",appointment);
        }
        return "appointment/input";
    }

    @RequestMapping("/saveOrUpdate")
    @RequiredPermission(name="预约新增或修改",expression = "appointment:saveOrUpdate")
    public String saveOrUpdate(Appointment appointment){
        if (appointment.getId()!=null) {
            appointmentService.update(appointment);
        }else {
            appointment.setStatus(AppointmentStatusEnum.PERFORM.getStatus());
          appointmentService.save(appointment);
        }
        return "redirect:/appointment/list";
    }
    @RequestMapping("/updateStatus")
    public String updateStatus(Appointment appointment){
        appointmentService.updateStatus(appointment);
        return "redirect:/appointment/list";
    }





}
